package com.breaktian.assemble.controller;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Window;

import com.breaktian.assemble.R;


/**
 * 控制Fragment的添加删除等操作
 * <p/>
 */
public final class FragmentController {

    /**
     * FragmentActivity内第一次添加Fragment
     */
    public static void initFragment(FragmentManager fragmentManager, Fragment targetFragment, String tag) {
        initFragment(fragmentManager, targetFragment, tag, Window.ID_ANDROID_CONTENT);
    }

    /**
     * FragmentActivity内第一次添加Fragment
     */
    public static void initFragment(FragmentManager fragmentManager, Fragment targetFragment, String tag, int containerViewId) {
        boolean isReplace = fragmentManager.findFragmentById(containerViewId) != null;
        int animIn = isReplace ? R.anim.activity_open_enter : 0;
        int animOut = isReplace ? R.anim.activity_open_exit : 0;
        int animCloseIn = 0;
        int animCloseOut = 0;

        initFragment(fragmentManager, targetFragment, tag, containerViewId,
                animIn,
                animOut,
                animCloseIn,
                animCloseOut);
    }

    /**
     * FragmentActivity内第一次添加Fragment
     */
    public static void initFragment(FragmentManager fragmentManager, Fragment targetFragment, String tag, int containerViewId
            , int animIn, int animOut, int animCloseIn, int animCloseOut) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.setCustomAnimations(animIn, animOut, animCloseIn, animCloseOut);
        Fragment fragment = fragmentManager.findFragmentById(containerViewId);
        if (fragment != null) {
            transaction.replace(containerViewId, targetFragment, tag);
        } else {
            transaction.add(containerViewId, targetFragment, tag);
        }
        transaction.commitAllowingStateLoss();
    }


    /**
     * 添加Fragment
     */
    public static void addFragment(FragmentManager fragmentManager, Fragment targetFragment, String tag) {
        addFragment(fragmentManager, targetFragment, tag, Window.ID_ANDROID_CONTENT);
    }

    /**
     * 添加Fragment
     * 无动画
     */
    public static void addFragmentNoAnim(FragmentManager fragmentManager, Fragment targetFragment, String tag) {
        addFragment(fragmentManager, targetFragment, tag, Window.ID_ANDROID_CONTENT,0,0,0,0);
    }

    /**
     * 添加Fragment
     * 从下往上动画
     * 底部进入和底部收起
     */
    public static void addFragmentWithVerticalAnim(FragmentManager fragmentManager, Fragment targetFragment, String tag) {
        addFragment(fragmentManager, targetFragment, tag, Window.ID_ANDROID_CONTENT,
                R.anim.fragment_vertical_enter,R.anim.fragment_vertical_exit,R.anim.fragment_vertical_pop_enter,R.anim.fragment_vertical_pop_exit);
    }

    /**
     * 添加Fragment
     */
    public static void addFragment(FragmentManager fragmentManager, Fragment targetFragment, String tag, int containerViewId) {
        addFragment(fragmentManager, targetFragment, tag, containerViewId,
                R.anim.activity_open_enter,
                R.anim.activity_open_exit,
                R.anim.activity_close_enter,
                R.anim.activity_close_exit);
    }


    /**
     * 添加Fragment
     */
    public static void addFragment(FragmentManager fragmentManager, Fragment targetFragment, String tag, int containerViewId
            , int animIn, int animOut, int animCloseIn, int animCloseOut) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.setCustomAnimations(animIn, animOut, animCloseIn, animCloseOut);
        Fragment fragment = fragmentManager.findFragmentById(containerViewId);
        if (fragment != null) {
            if (fragment instanceof FragmentManager.OnBackStackChangedListener) {
                fragmentManager.addOnBackStackChangedListener((FragmentManager.OnBackStackChangedListener) fragment);
            }
            transaction.hide(fragment);
        }
        transaction.add(containerViewId, targetFragment, tag);
        transaction.addToBackStack(tag);
        transaction.commitAllowingStateLoss();
    }

    /**
     * 移除Fragment
     */
    public static void removeFragment(FragmentManager fragmentManager, String tag) {
        if (fragmentManager.findFragmentByTag(tag) != null) {
            fragmentManager.popBackStack(tag, FragmentManager.POP_BACK_STACK_INCLUSIVE);
        }
        Fragment targetFragment = fragmentManager.findFragmentByTag(tag);
        if (targetFragment != null) {
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.remove(targetFragment);
            fragmentTransaction.commitAllowingStateLoss();
            fragmentManager.executePendingTransactions();
        }
    }

    /**
     * 移除Fragment
     */
    public static void removeFragment(FragmentManager fragmentManager, Fragment targetFragment) {
        String tag = targetFragment.getTag();
        if (fragmentManager.findFragmentByTag(tag) != null) {
            fragmentManager.popBackStack(tag, FragmentManager.POP_BACK_STACK_INCLUSIVE);
        }
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.remove(targetFragment);
        fragmentTransaction.commitAllowingStateLoss();
        fragmentManager.executePendingTransactions();
    }

    private FragmentController() {
    }

}
